package com.lanou3g.com.study;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/17;
 * Otherwise, I don't know who created it either
 */

@WebServlet(name = "CookieServlet" ,urlPatterns = "/a/b/c/d/testCookie.do")
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("suibianxie",
                "value");
        // 添加cookie信息
        response.addCookie(cookie);

        // 取出浏览器中的cookie
        // 取出 浏览器中的 所有cookie
        Cookie[] cookies = request.getCookies();
        PrintWriter writer = response.getWriter();
        for (Cookie c : cookies) {
            String name = c.getName();
            String value = c.getValue();
            writer.println(name + "::" + value);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
